import { defineConfig } from "vite";
const path = require("path");
import { UnifiedViteWeappTailwindcssPlugin as uvtw } from "weapp-tailwindcss/vite";
import uni from "@dcloudio/vite-plugin-uni";
const isH5 = process.env.UNI_PLATFORM === "h5";
const isApp = process.env.UNI_PLATFORM === "app";
const WeappTailwindcssDisabled = isH5 || isApp;
const postcssPlugins = [require("autoprefixer")(), require("tailwindcss")()];
if (!isH5) {
  postcssPlugins.push(
    require("postcss-rem-to-responsive-pixel")({
      rootValue: 32,
      propList: ["*"],
      transformUnit: "%",
    })
  );
}

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    uni(),
    uvtw({
      disabled: WeappTailwindcssDisabled,
    }),
    // isH5 ? undefined : vwt(),
  ],
  resolve: {
    alias: {
      "/@": path.resolve("./src"),
      "@/": path.resolve("./src"),
    },
  },
  css: {
    postcss: {
      plugins: postcssPlugins,
    },
  },
  build: {
    // 修改静态资源输出目录的名称，默认为 'assets'
    assetsDir: "assets-h5",

    // 更细粒度地控制资源文件命名
    rollupOptions: {
      output: {
        assetFileNames: (assetInfo) => {
          let extType = assetInfo.name.split(".")[1];
          if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(extType)) {
            extType = "images";
          } else if (/woff2?|eot|ttf|otf/i.test(extType)) {
            extType = "fonts";
          } else {
            extType = "files";
          }
          return `assets-h5/${extType}/[name]-[hash][extname]`;
        },
      },
    },
  },
  // 省略其他配置内容
});
